package com.thomsonreuter.search.compare.comparision;

import com.thomsonreuter.search.compare.filter.FieldFilterHelper;
import org.json.JSONException;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.JSONCompareResult;
import org.skyscreamer.jsonassert.comparator.DefaultComparator;
import org.skyscreamer.jsonassert.comparator.JSONCompareUtil;

import java.util.Iterator;
import java.util.Set;

public class CatComparator extends DefaultComparator {
    public CatComparator(JSONCompareMode mode) {
        super(mode);
    }

    @Override
    protected void checkJsonObjectKeysActualInExpected(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) {
        Set<String> actualKeys = JSONCompareUtil.getKeys(actual);
        Iterator var6 = actualKeys.iterator();
        FieldFilterHelper helper = FieldFilterHelper.getInstance();
        while(var6.hasNext()) {
            String key = (String)var6.next();
            if(helper.ignoredField(key)){
                continue;
            }
            if (!expected.has(key)) {
                result.unexpected(prefix, key);
            }
        }

    }

    @Override
    protected void checkJsonObjectKeysExpectedInActual(String prefix, JSONObject expected, JSONObject actual, JSONCompareResult result) throws JSONException {
        Set<String> expectedKeys = JSONCompareUtil.getKeys(expected);
        Iterator var6 = expectedKeys.iterator();

        while(var6.hasNext()) {
            String key = (String)var6.next();
            if (FieldFilterHelper.getInstance().ignoredField(key)) {
                continue;
            }
            Object expectedValue = expected.get(key);
            if (actual.has(key)) {
                Object actualValue = actual.get(key);
                this.compareValues(JSONCompareUtil.qualify(prefix, key), expectedValue, actualValue, result);
            } else {
                result.missing(prefix, key);
            }
        }

    }

}
